/*******************************************************************************
* Copyright (c) 2012 
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* zhangliang - initial API and implementation
*******************************************************************************/

#ifndef HANDLE_H
#define HANDLE_H

/*!
 * @brief
 *  handle of service
 * @note
 *  format : fff fffff
 *           --- -----
 *  svr id----|    |---- serial number alloc by local Services 
 */
class Handle {
 public:
  static Handle New(uint32_t serial_number);
  static uint32_t MaxSN(void)    { return kValMask; }
  static uint32_t MaxSvrId(void) { return kSvrIdMask >> kShift; }
  static Handle kInvalid;

 public:
  explicit Handle(void) : value_(-1) {}
  explicit Handle(uint32_t value);
  explicit Handle(uint32_t svr_id, uint32_t serial_number);

  uint32_t svr_id(void) const        { return value_ >> kShift; }
  uint32_t serial_number(void) const { return value_ & kValMask; }
  uint32_t value(void) const         { return value_; }

  bool operator==(const Handle& rhs) const { return value_ == rhs.value_; }
  operator bool(void) const { return !(*this == Handle::kInvalid); }
  std::string tostring(void) const { 
    return boost::lexical_cast<std::string>(value_);
  }

 private:
  enum {
    kValMask = 0x000fffff,
    kSvrIdMask = 0xfff00000,
    kShift = 20,
  };

  void Init(uint32_t svr_id, uint32_t serial_number);

  uint32_t value_;
};

#endif

